package com.iemes.controller.hr;
/**
 * 宿舍电费统计
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.DockFormMap;
import com.iemes.entity.ElectricityFormMap;
import com.iemes.entity.Electricity_groupFormMap;
import com.iemes.entity.Electricity_group_peopleFormMap;
import com.iemes.mapper.hr.DepartmentinfoMapper;
import com.iemes.mapper.hr.ElectricityMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("/hrelectricity/")
public class ElectricityController extends BaseController{

	@Inject
	private ElectricityMapper electricityMapper;
	@Inject
	private DepartmentinfoMapper departmentinfoMapper;
	//查看电费
	@RequestMapping("electricityAttend")
	public String examineOrder(Model model){
		DepartmentForMap departmentForMap=getFormMap(DepartmentForMap.class);
		departmentForMap.put("where", "where id!=16 ");
		//查部门名称
		List<DepartmentForMap> department=departmentinfoMapper.findByWhere(departmentForMap);
        model.addAttribute("caset",department);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/hr/electricity_list";
	}
	//电费分页
	@ResponseBody
	@RequestMapping("electricity_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		ElectricityFormMap electricityFormMap=getFormMap(ElectricityFormMap.class);
		List<Map<String, Object>> electricityFormMaps=electricityMapper.getAllElectricity(electricityFormMap);
		String dname=getPara("dname");
		
		for(Map<String, Object> map:electricityFormMaps){
			if(map.get("people_id")==null){
				continue;
			}else{
			String people_id=map.get("people_id").toString();
			List<Map<String,Object>> staffMaps=electricityMapper.getAllElectricityPeople(people_id,dname);
			map.put("staffMaps", staffMaps);
			}
			
		}
		LuogicPaging(pageNow, pageSize, electricityFormMaps);
		pageView.setRecords(electricityFormMaps);
		return pageView;
	}
	

	//编辑电费页面
	@RequestMapping("electricity_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		//电费统计
		ElectricityFormMap electricityFormMap=electricityMapper.findbyFrist("id", id, ElectricityFormMap.class);
		String group_id= electricityFormMap.get("group_id").toString();	
		Electricity_group_peopleFormMap electricity_group_peopleFormMap=getFormMap(Electricity_group_peopleFormMap.class);
		electricity_group_peopleFormMap.put("where", "where electricity_group_id="+group_id);
		//字典
		List<Electricity_group_peopleFormMap> electricity_group_peopleFormMaps=electricityMapper.findByWhere(electricity_group_peopleFormMap);
		List<String> peopleId=new ArrayList<String>();
		Integer count=0;
		for(Electricity_group_peopleFormMap dock:electricity_group_peopleFormMaps){
			if(dock.get("people_id")==null){
				continue;
			}else{
				peopleId.add(dock.get("people_id").toString());
				count+=1;
			}
		
		}
		//员工信息和部门
		List<Map<String,Object>> staffFormMap=electricityMapper.getAllPeople(peopleId);	
		model.addAttribute("electri",electricityFormMap);
		model.addAttribute("staff",staffFormMap);
		model.addAttribute("count",count);
		return Common.BACKGROUND_PATH + "/hr/electricity_edit";
	}
	//修改电费操作
	@ResponseBody
	@RequestMapping(value="electricity_editEntity")
	@SystemLog(module="电费数据维护",methods="查看电费-修改电费")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() {
		//获取jsp动态生成的文本框的值
		/*HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();*/
		/*String[] job_number=request.getParameterValues("staffFormMap.job_number");*/
		/*String[] peopleId=request.getParameterValues("staffFormMap.ID");//获取同宿舍下的员工的id
*/		ElectricityFormMap electricityFormMap=getFormMap(ElectricityFormMap.class);
		try {
			electricityMapper.editEntity(electricityFormMap);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
		
	//新增宿舍员工页面
	@RequestMapping("electricityPeople_add")
	public String addUI(Model model) throws Exception {
		String id=getPara("id");//电费表的id
		model.addAttribute("id",id);
		return Common.BACKGROUND_PATH + "/hr/electricityPeople_add";
	}
	
	//新增宿舍员工操作
	@ResponseBody
	@RequestMapping("electricityPeople_addEntity")
	@SystemLog(module="宿舍员工管理",methods="宿舍员工管理-新增宿舍员工")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() {
		Electricity_group_peopleFormMap electricity_group_peopleFormMap=getFormMap(Electricity_group_peopleFormMap.class);
		//获取jsp动态生成的文本框的值
		  HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		  String[] job_number=request.getParameterValues("job_number");
		  List<Integer> staffId=new ArrayList<Integer>();
		  //通过工号查询出人员id
          staffId=electricityMapper.getAllPeopleinfo(job_number);
          DockFormMap dockFormMap=getFormMap(DockFormMap.class);
         
		try {  
			for (Integer ID : staffId) {
				//通过员工外键得到对应收费字典数据
				DockFormMap count = electricityMapper.getDock(ID.toString());
				// 获取收费字典的id
				String dockId = count.get("id").toString();
				// 获取电费表的id
				String id = getPara("electricityid");
				//通过对应的id获取对应的电费数据
				ElectricityFormMap electricityFormMap=electricityMapper.findbyFrist("id", id, ElectricityFormMap.class);
				//获取对应的宿舍外键id
				String group_id= electricityFormMap.get("group_id").toString();
				dockFormMap.put("id", dockId);
				dockFormMap.put("electricity_id", id);
				electricityMapper.editEntity(dockFormMap);
				//查询字典表最大值
				Integer electricityPeopleId=electricityMapper.maxElectricityPeople();
				electricityPeopleId+=1;
				electricity_group_peopleFormMap.put("id", electricityPeopleId);
				electricity_group_peopleFormMap.put("people_id", ID);
				electricity_group_peopleFormMap.put("electricity_group_id", group_id);
				electricityMapper.addEntity(electricity_group_peopleFormMap);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}			
	
	
			
	//删除宿舍员工页面
		@RequestMapping("electricityPeople_delete")
		@Transactional
		public String deleteUI(Model model) throws Exception {
			String id = getPara("id");
			//电费统计
			ElectricityFormMap electricityFormMap=electricityMapper.findbyFrist("id", id, ElectricityFormMap.class);
			String group_id= electricityFormMap.get("group_id").toString();	
			Electricity_group_peopleFormMap electricity_group_peopleFormMap=getFormMap(Electricity_group_peopleFormMap.class);
			electricity_group_peopleFormMap.put("where", "where electricity_group_id="+group_id);
			//字典
			List<Electricity_group_peopleFormMap> electricity_group_peopleFormMaps=electricityMapper.findByWhere(electricity_group_peopleFormMap);
			List<String> peopleId=new ArrayList<String>();
			Integer count=0;
			for(Electricity_group_peopleFormMap dock:electricity_group_peopleFormMaps){
				if(dock.get("people_id")==null){
					continue;
				}else{
					peopleId.add(dock.get("people_id").toString());
					count+=1;
				}
				
			}
			//员工信息和部门
			List<Map<String,Object>> staffFormMap=electricityMapper.getAllPeople(peopleId);	
			model.addAttribute("electri",electricityFormMap);
			model.addAttribute("staff",staffFormMap);
			model.addAttribute("count",count);
			model.addAttribute("id",id);
			return Common.BACKGROUND_PATH + "/hr/electricityPeople_delete";
		}
		
		//删除宿舍员工操作
		@ResponseBody
		@RequestMapping("electricityPeople_deleteEntity")
		@SystemLog(module="宿舍员工管理",methods="宿舍员工管理-删除宿舍员工")//凡需要处理业务逻辑的.都需要记录操作日志
		public String deleteEntity() {
			//获取jsp动态生成的文本框的值
			  HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
			  String[] job_number=request.getParameterValues("job_number");
			  List<Integer> staffId=new ArrayList<Integer>();
	          staffId=electricityMapper.getAllPeopleinfo(job_number);
	          
			try { 
				//这里是员工id
				for (Integer ID : staffId) {
					electricityMapper.deleteDocks(ID.toString());
					electricityMapper.deleteDock(ID.toString());

				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "false";
			}
			return "success";
		}
		// 宿舍详情页面
		@RequestMapping("electricity_particulars")
		public String examineOrders(Model model) {
			String id = getPara("id");
			//电费统计
			ElectricityFormMap electricityFormMap=electricityMapper.findbyFrist("id", id, ElectricityFormMap.class);
			String group_id= electricityFormMap.get("group_id").toString();	
			Electricity_group_peopleFormMap electricity_group_peopleFormMap=getFormMap(Electricity_group_peopleFormMap.class);
			electricity_group_peopleFormMap.put("where", "where electricity_group_id="+group_id);
			//字典
			List<Electricity_group_peopleFormMap> electricity_group_peopleFormMaps=electricityMapper.findByWhere(electricity_group_peopleFormMap);
			List<String> peopleId=new ArrayList<String>();
			Integer count=0;
			for(Electricity_group_peopleFormMap dock:electricity_group_peopleFormMaps){
				if(dock.get("people_id")==null){
					continue;
				}else{
					peopleId.add(dock.get("people_id").toString());
					count+=1;
				}
				
			}
			//员工信息和部门
			List<Map<String,Object>> staffFormMap=electricityMapper.getAllPeople(peopleId);	
			model.addAttribute("electri",electricityFormMap);
			model.addAttribute("staff",staffFormMap);
			model.addAttribute("count",count);
			return Common.BACKGROUND_PATH + "/hr/electricity_particulars";
		}

		
	
	//新增宿舍页面
	@RequestMapping("electricity_add")
	public String addUIs(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/hr/electricity_add";
	}
	
	//新增宿舍操作
	@ResponseBody
	@RequestMapping("electricity_addEntity")
	@SystemLog(module="订单管理",methods="订单管理-新增订单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntitys() {
		Electricity_groupFormMap electricity_groupFormMap=getFormMap(Electricity_groupFormMap.class);
		ElectricityFormMap electricityFormMap=getFormMap(ElectricityFormMap.class);
		Electricity_group_peopleFormMap electricity_group_peopleFormMap=getFormMap(Electricity_group_peopleFormMap.class);
        //查询宿舍编号的最大值
		Integer group=electricityMapper.maxgroup();		
		//查询宿舍电费表最大值 
		Integer electricityId=electricityMapper.maxElectricity();
		
		
		group+=1;
		electricityId+=1;
		
		String name=getPara("name");
		Electricity_groupFormMap electricity_groupFormMaps= electricityMapper.findbyFrist("name", name, Electricity_groupFormMap.class);
		//判断是否已存在
		if(electricity_groupFormMaps==null){
		electricity_groupFormMap.put("id", group);
		electricity_groupFormMap.put("name", name);
		electricityFormMap.put("id",electricityId);
		electricityFormMap.put("group_id",group);//电费表的宿舍外键
		electricity_group_peopleFormMap.put("electricity_group_id", group);
		}else{
			return "false";
		}
		try {  
				electricityMapper.addEntity(electricity_groupFormMap);
				electricityMapper.addEntity(electricityFormMap);
				electricityMapper.addEntity(electricity_group_peopleFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}		
	
	//删除宿舍
	@ResponseBody
	@RequestMapping("electricityDormitory_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="订单管理",methods="订单管理-删除订单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteDormitoryEntity() throws Exception {
			String[] ids = getParaValues("ids");//电费表id
			String Pid=null;
			for (String id : ids) {
				Pid=id;
				
			}
			String[] value=Pid.split(",");
			for (String string : value) {
				
				//查询电费表
				ElectricityFormMap electricityFormMap=electricityMapper.findbyFrist("id", string, ElectricityFormMap.class);
				//得到宿舍外键id
				String group_id=electricityFormMap.get("group_id").toString();
				
				//查询宿舍是否有人
				List<Electricity_groupFormMap>electricity_groupFormMap=electricityMapper.getDormitory(group_id);
				if(electricity_groupFormMap.size()==1&&null==electricity_groupFormMap.get(0)||electricity_groupFormMap.size()==0){					//删电费表
					electricityMapper.deleteOrders(string);
					//删宿舍人员字典
					electricityMapper.deleteByAttribute("electricity_group_id", group_id, Electricity_group_peopleFormMap.class);
					//删宿舍
					electricityMapper.deleteOrder(group_id);
				}else{
					return "false";
				}
				
			}
			return "success";
	}

	
	
}
